package com.meixiang.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.meixiang.R;
import com.meixiang.activity.account.myfund.FundDetialActivity;
import com.meixiang.activity.account.myfund.MyFundActivity;
import com.meixiang.adapter.myFundAdapter.FundHoldAdapter;
import com.meixiang.adapter.myFundAdapter.FundHoldHistoryAdapter;
import com.meixiang.entity.fund.FundAccountBean;
import com.meixiang.entity.fund.FundAccountHistoryResult;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.util.AbJsonUtil;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/5/18.
 * 弹出 popupWindow 层
 */
public class HoldHistoryPopup extends PopupWindow implements View.OnClickListener{

    private FundHoldHistoryAdapter fundHoldHistoryAdapter ;
    private LinearLayout tv_fund_history_hold_up ;
    private RecyclerView swipe_target ;
    private TextView tv_fund_no_data ;
    private View parentView  ; //父类组件
    private Context mContext ;
    private Activity activity ;
    private List<FundAccountBean.FundHoldingList> fund_his_holding_list ; //历史持有基金数据

    public HoldHistoryPopup(Context context, View parentView, Activity activity){
        this.parentView = parentView ;
        this.mContext = context ;
        this.activity = activity ;
        initView();
        initData();
        initScreenData();
        getHttpFundData();
    }
    private void initData(){
        this.setContentView(parentView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(ContextCompat.getDrawable(mContext,R.mipmap.coupon2));
        this.setAnimationStyle(R.style.hold_history_anim_style); //设置弹出动画
    }

    /**
     * 获取我的历史持有基金数据
     */
    private void getHttpFundData() {

        HttpUtils.post(Config.FUND_ACCOUNT_HISTORY, MyFundActivity.TAG,null,new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) { //数据获取成功
                FundAccountHistoryResult fundAccountHistoryResult = AbJsonUtil.fromJson(result.optString("data"),new TypeToken<FundAccountHistoryResult>(){});
                if(fundAccountHistoryResult !=null&&fundAccountHistoryResult.getFund_his_holding_list().size()!=0){
                    tv_fund_no_data.setVisibility(View.GONE);
                    swipe_target.setVisibility(View.VISIBLE);
                    fund_his_holding_list = fundAccountHistoryResult.getFund_his_holding_list() ;
                    fundHoldHistoryAdapter.refreshData(fund_his_holding_list);
                }else{
                    tv_fund_no_data.setVisibility(View.VISIBLE);
                    swipe_target.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String code, String message) {
            }
        });
    }

    /**
     * 初始化筛选数据
     */
    private void initScreenData(){
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            swipe_target.setLayoutManager(mLayoutManager);
            swipe_target.setItemAnimator(new DefaultItemAnimator());
            fundHoldHistoryAdapter = new FundHoldHistoryAdapter(mContext);
            swipe_target.setAdapter(fundHoldHistoryAdapter);
        fundHoldHistoryAdapter.setOnItemClickListener(new FundHoldHistoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(mContext, FundDetialActivity.class);
                intent.putExtra("id", fund_his_holding_list.get(position).getId());
                intent.putExtra("titleName", fund_his_holding_list.get(position).getFund_name());
                intent.putExtra("fund_id", fund_his_holding_list.get(position).getFund_id());
                mContext.startActivity(intent);
            }
        });
            tv_fund_history_hold_up.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
    }

    private void initView(){
        tv_fund_history_hold_up = (LinearLayout) parentView.findViewById(R.id.tv_fund_history_hold_up);
        swipe_target = (RecyclerView) parentView.findViewById(R.id.swipe_target);
        tv_fund_no_data = (TextView) parentView.findViewById(R.id.tv_fund_no_data);
        tv_fund_history_hold_up.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_fund_history_hold_up:
                dismiss();
                break;
        }

    }
}
